{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据类型与变量\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 输入和输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, 好\n"
     ]
    }
   ],
   "source": [
    "name = input('please enter your name: ')#中间可以加输入的提示信息\n",
    "print('hello,', name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 整数\n",
    "* 1000=100_00 中间加下划线并不影响  \n",
    "* 0x 表示16进制数\n",
    "### 浮点数\n",
    "* 1.23x109就是1.23e9，或者12.3e8，0.000012可以写成1.2e-5，等等\n",
    "### 字符串\n",
    "* 如果在字符串内部有\" '等符号，可以用转义字符\\来标识"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('I\\'m ok.')\n",
    "print('I\\'m learning\\nPython.')\n",
    "print('\\\\\\n\\\\')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 如果字符串里面有很多字符都需要转义，就需要加很多\\，为了简化，Python还允许用r''表示''内部的字符串默认不转义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('\\\\\\t\\\\')\n",
    "\n",
    "print(r'\\\\\\t\\\\')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 如果字符串内部有很多换行，用\\n写在一行里不好阅读，为了简化，Python允许用'''...'''的格式表示多行内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "line1\n",
      "line2\n",
      "line3\n"
     ]
    }
   ],
   "source": [
    "print('''line1\n",
    "... line2\n",
    "... line3''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 布尔值\n",
    "* 真为True，假为False，其他没啥好说的\n",
    "### 变量\n",
    "**例子**\n",
    "1. 在内存中创建了一个'ABC'的字符串；\n",
    "2. 在内存中创建了一个名为a的变量，并把它指向'ABC'。\n",
    "3. b指向a指向的字符串'ABC'\n",
    "4. a指向了新字符串'XYZ'\n",
    "5. 所以b还是'ABC'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ABC\n"
     ]
    }
   ],
   "source": [
    "a = 'ABC'\n",
    "b = a\n",
    "a = 'XYZ'\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串和编码\n",
    "### 字符编码\n",
    "在计算机内存中，统一使用Unicode编码，当需要保存到硬盘或者需要传输的时候，就转换为UTF-8编码。  \n",
    "\n",
    "用记事本编辑的时候，从文件读取的UTF-8字符被转换为Unicode字符到内存里，编辑完成后，保存的时候再把Unicode转换为UTF-8保存到文件：  \n",
    "![](2022-01-13-12-06-48.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### f-string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The area of a circle with radius 2.5 is 19.62\n"
     ]
    }
   ],
   "source": [
    "r = 2.5\n",
    "s = 3.14 * r ** 2#两个*代表幂次\n",
    "print(f'The area of a circle with radius {r} is {s:.2f}')#.2f代表保留两位小数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用list和tuple\n",
    "* 如果要取最后一个元素，除了计算索引位置外，还可以用-1做索引，直接获取最后一个元素,同理可以获取倒数第3个元素，，但是到第4个就越界了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tracy'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates = ['Michael', 'Bob', 'Tracy']\n",
    "classmates[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 插入元素到指定位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Jack', 'Jack', 'Bob', 'Tracy']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates.insert(1, 'Jack')\n",
    "classmates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 删除指定位置的元素，用pop(i)，不含参数就弹出最后一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates.pop(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**很神奇的是list里面的数据类型可以是不同的，甚至可以说另一个list**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = ['python', 'java', ['asp', 'php'], 'scheme']\n",
    "len(s)\n",
    "4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'php'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(s[3][2]) #读取第4个数据的第三个字符\n",
    "s[2][1]#第3个元素里面的第二个元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 元组\n",
    "* 元组定义后就不能更改了\n",
    "* 关于元组定义歧义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "(1,)\n"
     ]
    }
   ],
   "source": [
    "t = (1)#这玩意代表一个数字1在号中，与t=1同理\n",
    "print(t)\n",
    "k=(1,)#所以这样定义元组可以消除歧义\n",
    "print(k)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 某种可变的tuple（其实是list在变）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'b', ['X', 'Y'])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = ('a', 'b', ['A', 'B'])\n",
    "t[2][0] = 'X'\n",
    "t[2][1] = 'Y'\n",
    "t"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 条件判断\n",
    "### if elif else\n",
    "* 很简单的条件判断没什么好说的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "kid\n"
     ]
    }
   ],
   "source": [
    "age = 3\n",
    "if age >= 18:\n",
    "    print('adult')\n",
    "elif age >= 6:\n",
    "    print('teenager')\n",
    "else:\n",
    "    print('kid')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 一个数据类型变换的实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "00前\n"
     ]
    }
   ],
   "source": [
    "s = input('birth: ')\n",
    "birth = int(s)#把str变量转为int型变量来判断\n",
    "if birth < 2000:\n",
    "    print('00前')\n",
    "else:\n",
    "    print('00后')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环 \n",
    "### 简单循环\n",
    "* range函数——可以生成一个整数序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(10))#生成0~9的序列\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 生成0-100的整数序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "sum = 0\n",
    "for x in range(101):\n",
    "    sum = sum + x\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 很简单的循环，有手就行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum = 0\n",
    "n = 99\n",
    "while n > 0:\n",
    "    sum = sum + n\n",
    "    n = n - 2\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用dict和set\n",
    "### dict（字典） 是一种时间换空间的算法\n",
    "* 为什么要用字典呢，因为list找对应的元素是一个一个从头对到尾，而字典是直接告诉你这个字在哪里，比如告诉你要找的字在3600位，你就不用把前3599个元素都对比一遍了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "95"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}\n",
    "d['Michael']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 字典的扩充，通过key放入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "90"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['Jack'] = 90\n",
    "d['Jack']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 后面定义的值会覆盖前面的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "88"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "d['Jack'] = 88\n",
    "d['Jack']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 扩充完的字典如下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Michael': 95, 'Tracy': 85, 'Jack': 88}"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d#运行这行代码即可显示"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 如何判断要找的key在不在字典中呢？用d.get是一个好方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'不在哦'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.get('Thomas', \"不在哦\")#其中第二个参数作用是在key不在字典的时候的返回值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 如果在的话，则返回该key的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "95"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.get('Michael', \"不在哦\")#Micheal在字典中，其值为95"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 要删除一个key，用pop(key)方法，对应的value也会从dict中删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Michael': 95, 'Tracy': 85}"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}#原本是这样的\n",
    "d.pop('Bob')#弹出bob，bob没了\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### set（可以看成数学意义上的无序和无重复元素的集合）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 重复元素在set中自动被过滤："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = set([1, 1, 2, 2, 3, 3])\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 增加和删除元素的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2, 3, 250}"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = set([1, 2, 3])\n",
    "s.add(250)\n",
    "s.remove(1)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 交集和并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 3}\n",
      "{1, 2, 3, 4}\n"
     ]
    }
   ],
   "source": [
    "s1 = set([1, 2, 3])\n",
    "s2 = set([2, 3, 4])\n",
    "print(s1 & s2)\n",
    "print(s1 | s2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 对象问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abc'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 'abc'#a指向字符串'abc'\n",
    "a.replace('a', 'A')#这里实际上是用a指向的字符串创建一个副本，然后把这个副本来修改，再输出结果\n",
    "a#所以a还是指向"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "2e918aaa81d99c652401bdd1a0c185581595fb477ac919641bd65261b5d7782a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('base': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
